package huoye.shops.huoye.home.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.BaseFragment;
import huoye.shops.huoye.home.model.MerchantCenter;
import huoye.shops.huoye.home.presenter.HomeDataShowPresenter;
import huoye.shops.huoye.home.view.IHomeDataShowView;
import huoye.shops.huoye.operation.activity.ManageSellActivity;
import huoye.shops.huoye.operation.activity.StatisticsActivity;
import huoye.shops.utils.StockUtils;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeDataShowFragment extends BaseFragment implements IHomeDataShowView {

    public static final int PAGE_TYPE_TODAY = 1;   //今日
    public static final int PAGE_TYPE_ALL = 2;   //总数据

    @BindView(R.id.statistice_tv_order_number) //订单总额
            TextView tvOrderNum;
    @BindView(R.id.statistice_tv_deal_number) //交易总额
            TextView tvDealNum;
    @BindView(R.id.statistice_tv_attention_user) //关注用户
            TextView tvAttentionUser;
    @BindView(R.id.statistice_tv_browse_number) //浏览次数
            TextView tvBrowseNum;
    @BindView(R.id.statistice_tv_rate_order) //下单率
            TextView tvRateOrder;
    @BindView(R.id.statistice_tv_rate_attention) //关注率
            TextView tvRateAttention;


    @OnClick(R.id.statistice_rv_ordre)
    void onOrderClick(){
        startActivity(ManageSellActivity.class);
    }

       @OnClick(R.id.statistice_rv_user)
    void onUserClick(){
        startActivity(StatisticsActivity.class);
    }



    private int mType;
    private HomeDataShowPresenter homeDataShowPresenter;

    public HomeDataShowFragment() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_home_data_show;
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {
        getType();
        homeDataShowPresenter = new HomeDataShowPresenter();
        homeDataShowPresenter.addTaskListener(this);
        homeDataShowPresenter.onStart();
        if (mType == PAGE_TYPE_TODAY) {
            homeDataShowPresenter.postMerchantCenter("Today");
        } else {
            homeDataShowPresenter.postMerchantCenter("All");
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        if(homeDataShowPresenter != null ){
            if (mType == PAGE_TYPE_TODAY) {
                homeDataShowPresenter.postMerchantCenter("Today");
            } else {
                homeDataShowPresenter.postMerchantCenter("All");
            }
        }
    }

    private void getType() {
        Bundle bundle = getArguments();
        mType = bundle.getInt(BaseActivity.EXTRAS_KEY_DATA);
    }


    public static Fragment getInstan(int type) {
        HomeDataShowFragment fragment = new HomeDataShowFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BaseActivity.EXTRAS_KEY_DATA, type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setData(MerchantCenter merchantCenter) {
        tvOrderNum.setText(merchantCenter.getOrderNum());

        tvDealNum.setText(StockUtils.moenyStyleNoZero(merchantCenter.getOrderMoney() + ""));


        BigDecimal focusNum = StockUtils.noDecimal(merchantCenter.getFocusNum());
        BigDecimal browseNum = StockUtils.noDecimal(merchantCenter.getBrowseNum());
        tvAttentionUser.setText(focusNum + "");
        tvBrowseNum.setText(browseNum + "");


        double place = merchantCenter.getPlaceOrderRate() * 100;
        double focus = merchantCenter.getFocusRate() * 100;



        tvRateOrder.setText(Integer.valueOf((int) place)+"%");
        tvRateAttention.setText(Integer.valueOf((int) focus)+"%");
    }
}
